package synchronizedTest;

import javax.sound.midi.Soundbank;

/**
 * ClassName Table
 * Description TODO
 * Author Hexiao
 * Date 2018/8/24 17:05
 * Version 1.0
 **/
public class Table {
    private int beans = 20;
    public synchronized int getBean(){
        if(beans==0){
            throw new RuntimeException("null");
        }
        Thread.yield();//线程切换
        return beans--;
    }
    public void buy(){
        try {
            Thread t=Thread.currentThread();
            System.out.println(t+"正在挑选");
            Thread.sleep(5000);
            synchronized (this){
                System.out.println(t+"正在试衣服");
                Thread.sleep(5000);
            }
            System.out.println(t+"结账离开");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}